/**
 * 
 */
package com.logistics.web;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.logistics.Pickup;
import com.logistics.PickupQueryParam;
import com.logistics.admin.AdminGenericBaseAction;
import com.logistics.service.PickupService;

/**
 * @author hzz
 *
 * 2014年3月29日
 */

@Component("pickupAdminAction")
@Scope("prototype")
public class PickupAdminAction extends AdminGenericBaseAction<Pickup, PickupService> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private PickupService service;
	
	@Autowired
	public void setService(PickupService service) {
		this.service = service;
	}

	@Override
	public PickupService getService() {
		return service;
	}
	
	private Pickup pickup;

	@Override
	protected Pickup initEntityForAdd() {
		return pickup;
	}
	
	private PickupQueryParam param;
	
	public PickupQueryParam getParam() {
		return param;
	}

	public void setParam(PickupQueryParam param) {
		this.param = param;
	}
	
	private List<Pickup> pickups;

	public List<Pickup> getPickups() {
		return pickups;
	}

	public void setPickups(List<Pickup> pickups) {
		this.pickups = pickups;
	}

	public String list() {
		if (param == null) {
			param = new PickupQueryParam();
		}
		
		param.setPaging(getPaging());
		pickups = service.query(param);
		
		return LIST;
	}

	public void pickuped() {
		if (StringUtils.isBlank(getId())) {
			outputInvalidStatus(500);
			return;
		}
		try {
			service.updatePickupStatus(getId());
		} catch (Exception e) {
			outputInvalidStatus(500);
			return;
		}
		outputSimpleSuccessJson();
	}
	
	public void needPickup() {
		List<Pickup> pickups = service.needRemind();
		JsonConfig config = getJsonConfig(new String[] {"weight", "description"});
		JSONArray jsonArray = JSONArray.fromObject(pickups, config);
		for (Pickup pickup : pickups) {
			pickup.setRemind(false);
		}
		
		service.updatePrintStatus(pickups);
		
		outputJsonWithRecordset(jsonArray);
	}
}
